package com.yiwei.ydd.fragment;

import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.ChatCenterActivity;
import com.yiwei.ydd.activity.LoginActivity;
import com.yiwei.ydd.activity.MyCommissionDetailActivity;
import com.yiwei.ydd.activity.MyCouponActivity;
import com.yiwei.ydd.activity.MyMessageTotalActivity;
import com.yiwei.ydd.activity.MyOilCardActivity;
import com.yiwei.ydd.activity.MyOrderActivity;
import com.yiwei.ydd.activity.MyWalletActivity;
import com.yiwei.ydd.activity.PersonalCenterActivity;
import com.yiwei.ydd.activity.ReturnCashActivity;
import com.yiwei.ydd.activity.SettingActivity;
import com.yiwei.ydd.activity.ShareIndexActivity;
import com.yiwei.ydd.activity.VipLevelUpTipsActivity;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.event.LoginSuccessEvent;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.BottomIconText;
import com.yiwei.ydd.view.ObservableScrollView;
import com.yiwei.ydd.view.V19FrameLayout;
import com.yiwei.ydd.view.V19RelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xsooy-pc on 18-4-4.
 */

public class MainMyFragment extends BaseFragment implements ObservableScrollView.ScrollViewListener {

    Unbinder unbinder;
    @BindView(R.id.txt_return_money)
    TextView txtReturnMoney;
    @BindView(R.id.txt_add_money)
    TextView txtAddMoney;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_login)
    TextView btnLogin;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.img_top_bg)
    ImageView imgTopBg;
    @BindView(R.id.img_icon)
    CircleImageView imgIcon;
    @BindView(R.id.txt_invite_code)
    TextView txtInviteCode;
    @BindView(R.id.btn_my_msg)
    ImageView btnMyMsg;
    @BindView(R.id.btn_my_wallet)
    BottomIconText btnMyWallet;
    @BindView(R.id.btn_my_oil)
    BottomIconText btnMyOil;
    @BindView(R.id.btn_my_youhui)
    BottomIconText btnMyYouhui;
    @BindView(R.id.btn_fanxian)
    LinearLayout btnFanxian;
    @BindView(R.id.btn_yongjin)
    LinearLayout btnYongjin;
    @BindView(R.id.txt_my_order)
    TextView txtMyOrder;
    @BindView(R.id.btn_order)
    LinearLayout btnOrder;
    @BindView(R.id.txt_my_vip)
    TextView txtMyVip;
    @BindView(R.id.btn_vip)
    LinearLayout btnVip;
    @BindView(R.id.txt_my_fanxian)
    TextView txtMyFanxian;
    @BindView(R.id.btn_my_fanxian)
    LinearLayout btnMyFanxian;
    @BindView(R.id.txt_my_share)
    TextView txtMyShare;
    @BindView(R.id.btn_my_share)
    LinearLayout btnMyShare;
    @BindView(R.id.txt_my_chat)
    TextView txtMyChat;
    @BindView(R.id.btn_my_chat)
    LinearLayout btnMyChat;
    @BindView(R.id.btn_my_setting)
    ImageView btnMySetting;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    @BindView(R.id.btn_my_msg_sec)
    ImageView btnMyMsgSec;
    @BindView(R.id.toolbar_layout)
    V19RelativeLayout toolbar;
    private int textSize36;
    private int textSize24;
    private float headerHeight;
    private float minHeaderHeight;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_main_my;
    }

    @Override
    protected void initView() {
        textSize36 = getResources().getDimensionPixelSize(R.dimen.x36);
        textSize24 = getResources().getDimensionPixelSize(R.dimen.x24);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            headerHeight = getResources().getDimensionPixelSize(R.dimen.y168);
        } else {
            headerHeight = getResources().getDimensionPixelSize(R.dimen.y128);
        }
        minHeaderHeight = getResources().getDimension(R.dimen.y98);
        scrollView.setScrollViewListener(this);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getUserInfo();
            }
        });


        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginSuccessEvent event) {
        getUserInfo();
    }

    public void setLogout() {
        btnLogin.setVisibility(View.VISIBLE);
        txtName.setText("");
        txtInviteCode.setText("");
        Spanned textOne = Spans.with(this).font("0\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                font("已返现金额(元)", textSize24, 0xffA6A6A6).
                build();
        txtReturnMoney.setText(textOne);

        Spanned textTwo = Spans.with(this).font("0\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                font("累计佣金额(元)", textSize24, 0xffA6A6A6).
                build();
        txtAddMoney.setText(textTwo);
        Glide.with(this).load(R.mipmap.img_default_head).into(imgIcon);
    }

    public void getUserInfo() {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        refresh.setRefreshing(true);
        Api.api_service.getUserInfo().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.getStatus().equals("00")) {
                        btnLogin.setVisibility(View.GONE);
                        UserInfoGlobal.saveUserInfo(result.datas);
                        if (!TextUtils.isEmpty(result.datas.username)) {
                            txtName.setText(result.datas.username);
                        } else {
                            txtName.setText(result.datas.phone);
                        }
                        txtInviteCode.setText("我的邀请码：" + result.datas.invite_code);
                        Spanned textOne = Spans.with(this).font(result.datas.isback_money + "\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                                font("已返现金额(元)", textSize24, 0xffA6A6A6).
                                build();
                        txtReturnMoney.setText(textOne);

                        Spanned textTwo = Spans.with(this).font(result.datas.commission + "\n", textSize36, 0xffFB5E01).style(Typeface.BOLD).
                                font("累计佣金额(元)", textSize24, 0xffA6A6A6).
                                build();
                        txtAddMoney.setText(textTwo);
                        Glide.with(this).load(result.datas.icon).error(R.mipmap.img_default_head).into(imgIcon);
                    } else {
                        setLogout();
                    }

                });
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserInfo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        super.onDestroyView();
    }


    @OnClick({R.id.btn_my_setting_sec, R.id.txt_name, R.id.txt_invite_code, R.id.btn_order, R.id.btn_vip, R.id.img_icon, R.id.btn_login, R.id.btn_my_setting, R.id.btn_my_wallet, R.id.btn_my_oil, R.id.btn_my_youhui, R.id.btn_my_msg, R.id.btn_my_msg_sec, R.id.btn_my_fanxian, R.id.btn_my_share, R.id.btn_my_chat})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_order:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyOrderActivity.class);
                break;
            case R.id.btn_vip:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), VipLevelUpTipsActivity.class);
//                Util.startActivity(getActivity(), VipPacketActivity.class);
                break;
            case R.id.img_icon:
            case R.id.txt_name:
            case R.id.txt_invite_code:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), PersonalCenterActivity.class);
                break;
            case R.id.btn_login:
                Util.startActivity(getContext(), LoginActivity.class);
                break;
            case R.id.btn_my_setting_sec:
            case R.id.btn_my_setting:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), SettingActivity.class);
                break;

            case R.id.btn_my_wallet:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyWalletActivity.class);
                break;
            case R.id.btn_my_oil:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyOilCardActivity.class);
                break;
            case R.id.btn_my_youhui:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyCouponActivity.class);
                break;
            case R.id.btn_my_msg:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyMessageTotalActivity.class);
                break;
            case R.id.btn_my_msg_sec:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), MyMessageTotalActivity.class);
                break;
            case R.id.btn_my_fanxian:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), ReturnCashActivity.class);
                break;
            case R.id.btn_my_share:

//                new SecurityPassDilaog(getContext()).show();
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), ShareIndexActivity.class);
                break;
            case R.id.btn_my_chat:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), ChatCenterActivity.class);
                break;

        }
    }

    @OnClick({R.id.btn_fanxian, R.id.btn_yongjin})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.btn_fanxian:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getActivity(), ReturnCashActivity.class);
                break;
            case R.id.btn_yongjin:
                if (Util.getIsLogin(getContext()))
                    Util.startActivity(getContext(), MyCommissionDetailActivity.class);
                break;
        }
    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
        //Y轴偏移量
        float scrollY = scrollView.getScrollY();
//        e3483d
        //变化率
        float headerBarOffsetY = headerHeight - minHeaderHeight;//Toolbar与header高度的差值
        float offset = 1 - Math.max((headerBarOffsetY - scrollY) / headerBarOffsetY, 0f);

        //Toolbar背景色透明度


        if (scrollY >= headerHeight) {
            toolbar.setVisibility(View.VISIBLE);
            toolbar.setAlpha(1);
//            toolbar.setBackgroundColor(Color.argb(255, 81, 119, 254));
//            if (toolbar.getVisibility() == View.GONE) {
//                return;
//            }
        } else {

//            if (layoutToolbar.getVisibility() == View.VISIBLE) {
//                layoutToolbar.setBackgroundColor(Color.argb((int) (offset * 255), 81, 119, 254));
//                return;
//            }
            if (scrollY == 0) {
                toolbar.setVisibility(View.GONE);
            } else {
                toolbar.setVisibility(View.VISIBLE);
                toolbar.setAlpha(offset);
//                toolbar.setBackgroundColor(Color.argb((int) (offset * 255), 81, 119, 254));
            }
        }
    }
}
